package com.example.demo.service.impl;

import com.example.demo.mapper.RechargeMapper;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.Recharge;
import com.example.demo.pojo.UtilityBill;
import com.example.demo.service.RechargeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;


@Service
public class RechargeServiceImpl implements RechargeService {

    @Autowired
    private RechargeMapper rechargeMapper;

    @Override
    public PageBean<Recharge> findByUserid(int userid, int pageNum, int pageSize) {
        // 使用PageHelper进行分页设置
        PageHelper.startPage(pageNum, pageSize);
        // 调用Mapper的方法查询数据
        List<Recharge> recharges = rechargeMapper.findByUserid(userid);
        // 使用PageInfo包装查询结果，这样便可以获取分页信息
        PageInfo<Recharge> pageInfo = new PageInfo<>(recharges);
        // 返回自定义的分页结果
        return new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
    }


    @Override
    public void rechargemoney(String paymentmethod, double amount,double balanceafter,int userid){
        String payid = UUID.randomUUID().toString().replaceAll("-", "");
        rechargeMapper.add(paymentmethod,amount,balanceafter,userid,payid);
    }

    @Override
    public PageBean<Recharge> findByrealname(String realname, int pageNum, int pageSize) {
        // 初始化PageBean对象，用于存放分页后的数据和分页信息
        PageBean<Recharge> pb = new PageBean<>();
        // 初始化分页插件，设置当前页码和每页显示的数量
        PageHelper.startPage(pageNum, pageSize);

        // 根据用户ID和类别ID查询公告列表
        List<Recharge> as = rechargeMapper.findByrealname(realname);
        // 将查询结果转换为Page对象，以获取总记录数等分页信息
        Page<Recharge> p = (Page<Recharge>) as;

        // 将分页信息设置到PageBean中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());

        // 返回包含公告分页信息的PageBean对象
        return pb;
    }

    @Override
    public Recharge getByrecordid(Integer recordid) {
        return rechargeMapper.getByrecordid(recordid);
    }

    @Override
    public void deleterecharge(Integer recordid) {
        rechargeMapper.deleterecharge(recordid);
    }

    @Override
    public void cancelrecharge(Integer recordid) {
        rechargeMapper.cancelrecharge(recordid);
    }

    @Override
    public Recharge getBypayid(String payid) {
        return rechargeMapper.getBypayid(payid);
    }

    @Override
    public void updatestatus(Integer recordid) {
        rechargeMapper.updatestatus(recordid);
    }


}
